﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExpressionEvaluator;
using System.Linq.Expressions;

namespace RemoteLinqBase.ServerBase
{
	public class GenericCollection<T>
	{
		private List<T> _InnerCollection;

		/// <summary>
		/// Initializes a new instance of the <see cref="GenericCollection&lt;T&gt;"/> class.
		/// </summary>
		public GenericCollection()
		{
			_InnerCollection = new List<T>();
		}

		/// <summary>
		/// Adds the specified item.
		/// </summary>
		/// <param name="item">The item.</param>
		public void Add(T item)
		{
			_InnerCollection.Add(item);
		}

		/// <summary>
		/// Gets the result of linq query.
		/// </summary>
		/// <param name="linq">The linq.</param>
		/// <returns>Result of linq query.</returns>
		public object GetResultLinq(string linq)
		{
			var Evaluator = new ExpressionEval(linq)
				.AddLookupAssembly("System.Core")
				.AddLookupNamespace("ExpressionEvalTest")
				.AddExtension("System.Linq", "IQueryable`1")
				.AddExtension("System.Linq", "IOrderedQueryable`1")
				.AddExtension("System.Linq", "IEnumerable`1")
				.AddParam<IQueryable<T>>("linq");
			LambdaExpression linqExpression = Evaluator.EvalLambda();
			object result = linqExpression.Compile().DynamicInvoke(_InnerCollection.AsQueryable());

			return result;
		}
	}
}
